package com.project.srtp.secure.controller;

import com.project.srtp.secure.dto.req.BusinessOperationReqDTO;
import com.project.srtp.secure.dto.resp.VerifiableLogRespDTO;
import com.project.srtp.secure.service.BusinessOperationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @author 曹行阳
 * @version 0.1
 * @date 2025/6/11 11:16
 */
@RestController
@RequestMapping("/api/v1/operations")
@RequiredArgsConstructor
public class BusinessOperationController {

    private final BusinessOperationService businessOperationService; // 业务操作处理服务

    /**
     * 提交业务操作请求
     *
     * @param reqDTO 包含业务操作数据的请求体
     * @return 包含可验证日志的响应实体（HTTP 200状态）
     * @throws NoSuchAlgorithmException 当请求的加密算法不可用时抛出
     * @throws NoSuchProviderException  当请求的安全提供者不可用时抛出
     *                                  功能说明：
     *                                  1. 接收客户端提交的业务操作请求
     *                                  2. 通过业务操作服务处理请求
     *                                  3. 返回包含可验证日志的响应
     */
    @PostMapping
    public ResponseEntity<VerifiableLogRespDTO> submitOperation(
            @RequestBody BusinessOperationReqDTO reqDTO)
            throws NoSuchAlgorithmException, NoSuchProviderException {

        // 调用服务层处理业务操作
        VerifiableLogRespDTO response = businessOperationService.processOperation(reqDTO);

        // 返回HTTP 200响应及处理结果
        return ResponseEntity.ok(response);
    }
}
